﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kardex_BussinesLogic;
using System.Data.Entity;
using System.Reflection;
using System.Diagnostics;
using KardexSist.UserControls.Common;

namespace KardexSist
{
    public partial class FrmMain : Form
    {
        List<object> lstControls = new List<object>();
        public FrmMain()
        {
            InitializeComponent();
            this.trlMenuControls.ExpandAll();
        } 

        private void FrmMain_Load(object sender, EventArgs e)
        {

        }

        private void trlMenuControls_NodeChanged(object sender, DevExpress.XtraTreeList.NodeChangedEventArgs e)
        {

        }

        private void trlMenuControls_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                Assembly asmControl = Assembly.Load("KardexSist");
                Object objControl = asmControl.CreateInstance(e.Node.GetValue(2).ToString());

                if (!ControlLoaded(objControl))
                {
                    CtrlMainBase ctrl = (CtrlMainBase)objControl;
                    ctrl.Dock = DockStyle.Fill;
                    ctrl.TitleControl = e.Node.GetValue(1).ToString();
                    pnlMainControls.Controls.Add(ctrl);
                    lstControls.Add(objControl);
                }

                ShowAndHideControl(objControl);

            }
            catch (Exception ex)
            {
                Trace.Write("error" + ex.Message);

                //throw;
            }
        }

        private void ShowAndHideControl(object objControl)
        {
            foreach (Object item in lstControls)
            {
                UserControl ctrl = (UserControl)item;
                ctrl.Visible = false;
                if (objControl.ToString().Equals(item.ToString()))
                { 
                    ctrl.Visible = true;
                }
            }  
        }
        private bool ControlLoaded(Object objControl)
        {
            foreach (Object item in lstControls) 
            {
                if (objControl.ToString().Equals(item.ToString()))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
